(function(){
	
	// 验证
	if(!localStorage.getItem('token')){
		alert('非法登录！');
		location.href = 'index.html';
		return;
	};
	
	
	// 获取订单号
	let tradeNo = wj.getUrlValue('trade_no');

	// 查询支付结果
	wj.ajax({
		method: 'post',
		url: BASE_URL + '/api_payquery',
		data: {
			userId: localStorage.getItem('token'),
			tradeNo,
		},
		success(res){
			if(res.code != 0){
				console.log(res);
				return;
			};
			// 查询成功，获取用户订单列表
			getOrderList();
		}
	});
	
	// 页面刷新获取订单列表
	getOrderList();
	
	// 订单列表
	function getOrderList(){
		let oOrderList = document.querySelector('.order-list');
		
		oOrderList.innerHTML = '';
		
		wj.ajax({
			method: 'post',
			url: BASE_URL + '/api_order',
			data: {
				status: 'vieworder',
				userId: localStorage.getItem('token')
			},
			success(res){
				if(res.code != 0){
					console.log(res);
					return;
				};
				console.log(res);
				// DOM
				let orderData = res.data;
				
				orderData.forEach(item => {
					
					let strLi = '';
					let strGoods = '';
					let priceAll = 0;
					// 商品遍历
					item.goods.forEach(goodsItem => {
						strGoods += `
							<div class="goods-list">
								<img src="${goodsItem.goods_thumb}" alt="" />
								<span>${goodsItem.goods_name}</span>
								<span>${goodsItem.price}</span>
								<span>${goodsItem.goods_number}</span>
								<span>${parseInt(goodsItem.goods_number) * goodsItem.price}</span>
							</div>
						`;
						priceAll += parseInt(goodsItem.goods_number) * goodsItem.price;
					});
					
					strLi += strGoods;
					strLi += `
						<div class="price-all">${priceAll}</div>
						<div class="addrsss">${JSON.stringify(item.order_address)}</div>
						<div class="order-id">${item.trade_no}</div>
						<div class="order-status">${item.status}</div>
						<div class="del-order" trade-no="${item.trade_no}">删除按钮</div>
					`;
					
					let li = document.createElement('li');
					li.innerHTML = strLi;
					oOrderList.appendChild(li);
				});
				
				// 调用头部购物车渲染方法
				getCartValue();
				
				// 选择删除按钮
				let aDelBtn = document.querySelectorAll('.order-list .del-order');
				aDelBtn.forEach(item => {
					
					item.onclick = function(){
						wj.ajax({
							method: 'post',
							url: BASE_URL + '/api_order',
							data: {
								status: 'delorder',
								userId: localStorage.getItem('token'),
								tradeNo: this.getAttribute('trade-no')
							},
							success: res => {
								console.log(res);
								if(res.code != 0){
									console.log(res);
									return;
								};
								this.parentNode.remove();
							}
						})
					};
				})
			},
			
		});
	};
	
	
	// http://127.0.0.1:8848/lc/order.html?charset=utf-8&out_trade_no=1736211406406&method=alipay.trade.page.pay.return&total_amount=123.00&sign=h%2BD9DYzktY04K%2FsSpg5kKdI4E2lFnWtjsz%2FiF%2FIfXjsh4sODMra0Cx%2FTscKA6ULfqsURWGBZEdOJiyVJI90uTfvyWXJw6%2F4%2Fec%2BOyCldX9BOzFVfsozitXhQJm%2BWOb%2FPGMmCjjU9kwP6j7kjgNxkmccIORp9elcJiRyStnsG%2BT9Eh90TsQOeRhzncNRN%2FeL7d8kqDftR5VYV6yqMpHZSNqkB72ZMURmhmr85d2VedZff36VXL%2BFyrkkO8iAsvJpSGJn4hO44mZOQV13NvDztqf%2B3DzVbiFr4JXeLBidDLOEyFnXX0wHtHqt8mJa5YXJjCWwsPYVLhUP4HkXrOhuS6g%3D%3D&trade_no=2025010722001491590504531591&auth_app_id=9021000131652571&version=1.0&app_id=9021000131652571&sign_type=RSA2&seller_id=2088721020663094&timestamp=2025-01-07+09%3A05%3A06
	
	
	
	
	
	
})();